
clear
set more off
capture log close
set seed 12345	
set matsize 10000
set maxvar 20000
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"

* set tc sample (ncg nbus base)
local ivsamp "ncg"
* average or not ("_avg" or "")
local ivavg ""


**********************************************
**  Prep stayers
**********************************************
use $DATA/scorp100p_entsep_file, clear
keep if year>=2010

sort firm_tin tin 
foreach j in 2010 2011 2016 {
	gen zz = year==`j'
	by firm_tin tin: egen c`j' = sum(zz)
	drop zz
}
gen stay = c2012==1 & c2011==1 & c2016==1 & c2010==1
tab year stay
keep if stay==1
gen wages_ft=.
replace wages_ft=wages if ft==1

collapse (count) n_stay=tin (mean) wages wages_ft (sum) n_stay_ft=ft tot_wages_stay=wages tot_wages_stay_ft=wages_ft, by(firm_tin year)

ren wages wages_stay
ren wages_ft wages_stay_ft

sort firm_tin year
tempfile tstay2016
save `tstay2016', replace

**********************
** prep main sample + merge on stayers
**********************
use $DATA/reg_file_firm, clear
sort firm_tin year
merge 1:1 firm_tin year using `tstay2016'
tab year _merge
*keep if _merge==3
*drop _merge
drop if _merge==2
*indicator for stayer firm
gen vv=_merge==3
tab year vv
bys firm_tin: egen vv7=sum(vv)
tab year vv7
gen firm_stay = vv7>0
drop vv vv7

*****
** prep for regs
*****
gen zz = 0
replace zz=n_stay_ft if year==2012
bys firm_tin: egen n_stay_ft_2012=sum(zz)
drop zz

sort firm_tin year
foreach v in wages_stay_ft tot_wages_stay_ft {
	by firm_tin: gen d`v'=`v'-`v'[_n-1]
}

foreach w in 1  {
	local ptop = 100-`w'
	local pbot = `w'
	foreach o in dtot_wages_stay_ft tot_wages_stay_ft {
		sort year `o'
		by year: egen zt=pctile(`o'), p(`ptop')
		by year: egen zb=pctile(`o'), p(`pbot')
		gen w`o'`ptop' = `o'
		replace w`o'`ptop' = zt if `o'>zt & `o'~=.
		replace w`o'`ptop' = zb if `o'<zb & `o'~=.
		drop zt zb
	}
}

sort firm_tin year
by firm_tin: gen dwtot_wages_stay_ft99 = wtot_wages_stay_ft99 - wtot_wages_stay_ft99[_n-1]


************************************************************************************************
************************************************************************************************
**  Main - liab_tot [Table 4]
************************************************************************************************
************************************************************************************************

******
**  reduced form - all [Table 4, ITT]
******
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local tt "tt1112_ncg"
local t "dpwliab_tot_iv_2012"
drop dd
gen dd=`t'*post
local dr1 "i.netinc_`baseyr'cat##i.post i.naics_`baseyr'##i.post i.cz_`baseyr'##i.post"
local events "`t' post dd"

foreach r in 1 {
	eststo r`r': quietly reghdfe dwages_stay_ft `events'  if (`tt'==1 | `tt'==0) & bal_liab==1  & year>=2012  & firm_stay==1, absorb(`dr`r'' firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/table4_stay_itt.csv", replace se ar2 mtitles( ) keep(dd) addnote("Each specification includes a different set of control variabels")
eststo clear

******
**  2sls - dd [Table 4, 2SLS]
******
local tt "tt1112_ncg"
drop dd
gen dd=`tt'*post
local dr1 "i.valaddpw_ft_2012cat##i.post i.netinc_`baseyr'cat##i.post i.naics_`baseyr'##i.post i.cz_`baseyr'##i.post"
local events "`tt' post dd"

* first stage
foreach r in 1 {
	quietly reg liab_tot `events' `dr`r'' if (`tt'==1 | `tt'==0) & year>=2011  & bal_liab==1 & firm_stay==1
	predict yhatdd`r',xb
}
*F-stat
di e(F)
*F-stat = 210.032

foreach r in 1 {
	local t "yhatdd`r'"
	drop dd
	gen dd=`t'*post
	local events "`t' post dd"

	eststo w99_r`r': quietly reg dwtot_wages_stay_ft99 `events' `dr`r'' if (`tt'==1 | `tt'==0) & year>=2012 & bal_liab==1  & firm_stay==1, vce(cluster firm_tin)
}
esttab using "$OUTPUT/table4_stay_2sls.csv", replace se ar2 mtitles( ) keep(dd) addnote("Each specification includes a different set of control variabels")
eststo clear
drop yhat* 

**************
**  Elasticity  [Table 4, elasticity (ITT)]
**************
local r1 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.state_`baseyr'##ib`baseyr'.year"
foreach v in wages_stay wages_stay_ft {
	gen ln`v' = ln(`v')
}
local trt "tt1112_ncg"
local t "dlnatr_tot_iv_2012"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

foreach r in 1 {
	eststo r`r': quietly reg lnwages_stay_ft `events' `r`r''   [aw=n_ft_lower_2012] if (`trt'==1 | `trt'==0)  & bal_liab==1 & firm_stay==1  & (year==2011 | year==2012 | year==2016), vce(cluster firm_tin)
}
esttab using "$OUTPUT/table4_stay_elasticity.csv", replace se ar2 mtitles( ) keep(t2016) addnote("Each specification includes a different set of control variabels")
eststo clear






clear
exit




